package com.yonyou.pmclouds.constructiondiary.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.constructiondiary.entity.ConstructionDiaryVO;
import com.yonyou.pmclouds.constructiondiary.mapper.ConstructionDiaryMapper;
import com.yonyou.pmclouds.constructiondiary.rmiitf.ConstructionDiaryQuery;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.pmpub.entity.QueryListParam;
import com.yonyou.pmclouds.pmpub.rmiitf.IPageQuerySerivce;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Slf4j
@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= ConstructionDiaryQuery.class)
public class ConstructionDiaryQueryImpl implements ConstructionDiaryQuery , IPageQuerySerivce<ConstructionDiaryVO> {

    @Autowired
    ConstructionDiaryMapper mapper;
    @Autowired
    private PictureRemoteService picService;
    @Autowired
    private ProjectSimpleQuery projectQuery;
    @Autowired
    private OrganizationQuery organizationQuery;

    @Override
    public ConstructionDiaryVO[] queryDiaryListInfo(String pkProject) throws BusinessException {
        if(StringUtils.isEmpty(pkProject)){
            return new ConstructionDiaryVO[0];
        }
        ConstructionDiaryVO[] vos =  mapper.getDiaryListInfo(pkProject);
        getPictures(vos);
        return vos;
    }

    @Override
    public ConstructionDiaryVO queryDetailByID(String id) throws BusinessException {
        if(StringUtils.isEmpty(id)){
            return null;
        }
        ConstructionDiaryVO constructionDiaryVO = mapper.getByID(id);
        if(constructionDiaryVO != null){
            constructionDiaryVO.setPictures(queryPics(id));
        }
        return setRefFiledInfo(constructionDiaryVO);
    }

    private String[] queryPics(String pkBill) {
        return picService.queryPicNamesByPkEntity(pkBill);
    }

    private ConstructionDiaryVO setRefFiledInfo(ConstructionDiaryVO constructionDiaryVO) throws BusinessException {
        if(constructionDiaryVO == null){
            return null;
        }
        //项目
        ProjectVO projectVO = projectQuery.queryProject(constructionDiaryVO.getPk_project());
        if(projectVO != null){
            constructionDiaryVO.setProjectCode(projectVO.getScode());
            constructionDiaryVO.setProjectName(projectVO.getSname());
        }
        String pkOrg = projectVO.getPkOrg();
        if(StringUtils.isNotEmpty(pkOrg)) {
            OrganizationVO organizationVO = organizationQuery.queryByPkSrc(RuntimeEnvironment.getTenantId(), pkOrg);
            constructionDiaryVO.setOrgName(organizationVO==null?null:organizationVO.getSname());
        }

        return constructionDiaryVO;
    }

    @Override
    public boolean queryRefByIds(String code, String[] ids) throws BusinessException {
        boolean flag = false;
        Integer count = mapper.selectRefByCode(code, ids);
        if(count != null && count > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public PageObject<Object> queryByCondition(String[] pkProject, String[] creators, String startDate, String endDate, int[] billStatus, int start, int size) throws BusinessException {

        int count = mapper.countByCondition(pkProject,creators,startDate,endDate,billStatus);

        ConstructionDiaryVO[] constructionDiaryVOs =
                mapper.queryByCondition(pkProject, creators, startDate, endDate, billStatus, start, size);

        return new PageObject<Object>(setRefFiledInfo(constructionDiaryVOs),count,start);
    }

    /**
     * 添加照片
     */
    private void getPictures(ConstructionDiaryVO[] vos) throws BusinessException {
        if(ArrayUtils.isEmpty(vos)) return;
        List<String> pkBills = new ArrayList<>();
        for(ConstructionDiaryVO vo : vos){
            if(vo != null && vo.getPkConstructiondiary() != null) pkBills.add(vo.getPkConstructiondiary());
        }
        if(pkBills.size() == 0) return;

        Map<String, List<String> > picMap = picService.queryPicsByPkEntitys(pkBills.toArray(new String[0]));
        if(picMap == null) return;

        for(ConstructionDiaryVO vo : vos){
            if(vo != null && picMap.get(vo.getPkConstructiondiary()) != null){
                vo.setPictures(picMap.get(vo.getPkConstructiondiary()).toArray(new String[0]));
            }
        }
    }

    /**
     * 处理参照字段，非必输项需校验空
     *
     * @param vos
     * @return
     * @throws BusinessException
     */
    private ConstructionDiaryVO[] setRefFiledInfo(ConstructionDiaryVO[] vos) throws BusinessException {
        if (ArrayUtils.isEmpty(vos)) {
            return new ConstructionDiaryVO[0];
        }

        Set<String> projectSet = new HashSet<String>();
        Set<String> pkSet = new HashSet<String>();

        for (ConstructionDiaryVO vo:
                vos) {
            projectSet.add(vo.getPk_project());
            pkSet.add(vo.getPkConstructiondiary());
        }

        Map<String,ProjectVO> projectVOMap = projectQuery.queryProjects(projectSet.toArray(new String[0]));
        Map<String,List<String>> picMap = picService.queryPicsByPkEntitys(pkSet.toArray(new String[0]));

        for (ConstructionDiaryVO vo:
                vos) {
            vo.setProjectName(projectVOMap.get(vo.getPk_project()) == null ? null:projectVOMap.get(vo.getPk_project()).getSname());
            vo.setProjectCode(projectVOMap.get(vo.getPk_project()) == null ? null:projectVOMap.get(vo.getPk_project()).getScode());
            vo.setPicNum(picMap.get(vo.getPkConstructiondiary()) == null ? 0 : picMap.get(vo.getPkConstructiondiary()).size());
        }

        return vos;
    }


    @Override
    public int countByProject(QueryListParam param) throws BusinessException{

        int count = mapper.countByCondition( new String[]{param.getPkProject()}, null, param.getStartDate(), param.getEndDate(), null);

        return count;
    }

    @Override
    public ConstructionDiaryVO[] queryByProject(QueryListParam param) throws BusinessException {
        ConstructionDiaryVO[] vos = mapper.queryByCondition(new String[]{param.getPkProject()}, null,  param.getStartDate(), param.getEndDate(), null,param.getPageStart(),param.getPageSize());
        getPictures(vos);
        return vos;
    }


    @Override
    public String getBillType() {
        return BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE;
    }
}
